import type { AxiosResponse } from 'axios';
import { BACKEND_ERROR_CODE, createFlatRequest, createRequest } from '@sa/axios';
import { useAuthStore } from '@/store/modules/auth';
import { $t } from '@/locales';
import { localStg } from '@/utils/storage';
import { getServiceBaseURL } from '@/utils/service';
import { handleRefreshToken, showErrorMsg } from './shared';
import type { RequestInstanceState } from './type';

const isHttpProxy = import.meta.env.DEV && import.meta.env.VITE_HTTP_PROXY === 'Y';
const { otherBaseURL } = getServiceBaseURL(import.meta.env, isHttpProxy);
const baseURL = 'http://big-event-vue-api-t.itheima.net';

export const request = createRequest<App.Service.DemoResponse>(
  {
    baseURL
  },
  {
    async onRequest(config) {
      const { headers } = config;

      // set token
      const token = localStg.get('token');
      const Authorization = token
        ? `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MTcyODYsInVzZXJuYW1lIjoiMXEydzNlNHIxcSIsIm5pY2tuYW1lIjoiIiwiZW1haWwiOiIiLCJpYXQiOjE3MjA1MDY3ODAsImV4cCI6MTcyMDU0Mjc4MH0.N8giJaN3CPqiINutgLZP4JeRCgSYfVhcc6VWBJLGABw`
        : null;
      Object.assign(headers, { Authorization });

      return config;
    }
  }
);
